Import Data

data <- read.csv("USA_GDP_Annual.csv")

Note: All values are measured in millions of USD.

Data Wrangling

Column Types & Removing Commas

# remove commas & convert to numeric
data$GDP <- as.numeric(gsub("," ,"" , data$GDP))
data$Consumption <- as.numeric(gsub("," ,"" , data$Consumption))
data$Investment <- as.numeric(gsub("," ,"" , data$Investment))
data$Net.Exports <- as.numeric(gsub("," ,"" , data$Net.Exports))
data$Government.Spending <- as.numeric(gsub("," ,"" , data$Government.Spending))

Keep Certain Years

# keep years that are multiples of 10
data10 <- subset(data, Year %% 10 == 0)

Add Proportions

# add proportion of consumption, investment, net exports, and government spending
data10 <- data10 %>% mutate(
  Consumption.prop = Consumption / GDP,
  Investment.prop = Investment / GDP,
  Net.Exports.prop = Net.Exports / GDP,
  Government.Spending.prop = Government.Spending / GDP
)

Create plotly Graphic

GDP by Year (Line)

GDP by Year (Bar)

Components of GDP by Year (Stacked)

Components of GDP by Year (Group)